package vaccine.appointment.system.common.advice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import vaccine.appointment.system.appointmentrecord.exception.AppointmentRecordException;
import vaccine.appointment.system.common.exception.GlobalException;
import vaccine.appointment.system.family.exception.FamilyException;
import vaccine.appointment.system.inventory.exception.InventoryException;
import vaccine.appointment.system.person.exception.PersonException;
import vaccine.appointment.system.user.exception.UserException;
import vaccine.appointment.system.common.exception.UtilException;
import vaccine.appointment.system.vaccinationsite.exception.VaccinationSiteException;
import vaccine.appointment.system.vaccine.exception.VaccineException;

/**
 * 统一异常处理类
 */
@Slf4j
@ControllerAdvice  //控制层通知
@ResponseBody  //返回的是数据，而不是页面，因此使用@ResponseBody
public class ExceptionAdvice {
    //捕获 PersonException
    @ExceptionHandler(PersonException.class)
    public CommonResult<String> handlePersonException(PersonException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获 UserException
    @ExceptionHandler(UserException.class)
    public CommonResult<String> handleUserException(UserException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获FamilyException
    @ExceptionHandler(FamilyException.class)
    public CommonResult<String> handleFamilyException(FamilyException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获 VaccinationSiteException
    @ExceptionHandler(VaccinationSiteException.class)
    public CommonResult<String> handleVaccinationSiteException(VaccinationSiteException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获 VaccineException
    @ExceptionHandler(VaccineException.class)
    public CommonResult<String> handleVaccineException(VaccineException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获 InventoryException
    @ExceptionHandler(InventoryException.class)
    public CommonResult<String> handleInventoryException(InventoryException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获 AppointmentRecordException
    @ExceptionHandler(AppointmentRecordException.class)
    public CommonResult<String> handleAppointmentRecordException(AppointmentRecordException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获UtilException
    @ExceptionHandler(GlobalException.class)
    public CommonResult<String> handleGlobalException(GlobalException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获 UtilException
    @ExceptionHandler(UtilException.class)
    public CommonResult<String> handleUtilException(UtilException e) {
        log.error("发生异常，错误码：{}， 错误信息：{}", e.getCode(), e.getMsg());
        return CommonResult.fail(e.getCode(), e.getMsg());
    }

    //捕获其他异常，保底机制
    @ExceptionHandler(Exception.class)
    public CommonResult<String> handleException(Exception e) {
        // 如果是 CommonException 或 GlobalException，使用其 msg 字段
        if (e instanceof vaccine.appointment.system.common.exception.CommonException) {
            vaccine.appointment.system.common.exception.CommonException ce = 
                (vaccine.appointment.system.common.exception.CommonException) e;
            log.error("发生异常，错误码：{}， 错误信息：{}", ce.getCode(), ce.getMsg());
            return CommonResult.fail(ce.getCode(), ce.getMsg());
        } else if (e instanceof GlobalException) {
            GlobalException ge = (GlobalException) e;
            log.error("发生异常，错误码：{}， 错误信息：{}", ge.getCode(), ge.getMsg());
            return CommonResult.fail(ge.getCode(), ge.getMsg());
        } else {
            // 其他异常，使用 getMessage()
            String message = e.getMessage();
            if (message == null || message.isEmpty()) {
                message = e.getClass().getSimpleName();
            }
            log.error("发生异常：{}", message, e);
            return CommonResult.fail(500, "系统异常：" + message);
        }
    }
}
